/* Copyright (c) 2009 - 2010 98Labs Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sbal89.map.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sbal89.map.util.SDAUtil;

@SuppressWarnings("serial")
public class CountServlet extends BaseServlet {
	private Logger log = Logger.getLogger(this.getClass().getName());
	
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Integer userCount = 0;
		Integer mapCount = 0;
		
		try {
			userCount = SDAUtil.getUserCount(getSprService()); 
		} catch (Exception e) {
			
		}
		try {
			mapCount = SDAUtil.getMapCount(getSprService()); 
		} catch (Exception e) {
			
		}
		String xml = "<election-exchange>"
			+ "<users>"+ userCount + "</users>"
			+ "<maps>"+ mapCount + "</maps>"
			+ "</election-exchange>";
		
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "-1");
		response.setContentType("text/xml");
//		response.setContentType("application/json");
		PrintWriter pw = response.getWriter();
		pw.print(xml);
		pw.flush();
		log.info("doget: " + xml);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		log.info("dopost");
		doGet(request, response);
	}
}
